package wiki.zimo.servlet.backstage;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import wiki.zimo.constant.Constant;
import wiki.zimo.dao.CategoryDao;
import wiki.zimo.dao.OrderDao;
import wiki.zimo.dao.ProductDao;
import wiki.zimo.dao.UserDao;
import wiki.zimo.domain.Category;
import wiki.zimo.domain.Product;
import wiki.zimo.domain.User;
import wiki.zimo.servlet.BaseServlet;
import wiki.zimo.utils.JsonUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.List;

/**
 * @version v1.0
 * @auther: 子墨
 * @datetime: 2018/11/9 11:23
 * @description: 处理后台首页 -> 统计的Servlet
 */
@WebServlet(value = "/statistics")
public class StatisticsServlet extends BaseServlet {

    private UserDao userDao = new UserDao();
    private ProductDao productDao = new ProductDao();
    private CategoryDao categoryDao = new CategoryDao();
    private OrderDao orderDao = new OrderDao();

    /**
     * @auther: 子墨
     * @date: 2018/11/9 16:30
     * @describe: 后台首页
     * @param: [request, response]
     * @return: java.lang.String
     * @version v1.0
     */
    @Override
    public String index(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        HttpSession session = request.getSession();
        // 最新商品
        List<Product> newestProduct = productDao.findNewestProducts();
        session.setAttribute(Constant.PRODUCT_NEWEST, newestProduct);

        // 最热商品
        List<Product> hotestProduct = productDao.findHotestProducts();
        session.setAttribute(Constant.PRODUCT_HOTEST, hotestProduct);
        return Constant.URL_BACKSTAGE_INDEX;
    }

    /**
     * @auther: 子墨
     * @date: 2018/11/9 11:29
     * @describe: 获取商品数量接口
     * @param: [request, response]
     * @return: java.lang.String
     * @version v1.0
     */
    public String getProductCount(HttpServletRequest request, HttpServletResponse response) throws Exception {
        int count = productDao.count();
        response.getWriter().print(count);
        return null;
    }

    /**
     * @auther: 子墨
     * @date: 2018/11/9 11:31
     * @describe: 获取商品分类数量接口
     * @param: [request, response]
     * @return: java.lang.String
     * @version v1.0
     */
    public String getCategoryCount(HttpServletRequest request, HttpServletResponse response) throws Exception {
        int count = categoryDao.count();
        response.getWriter().print(count);
        return null;
    }

    /**
     * @auther: 子墨
     * @date: 2018/11/9 11:32
     * @describe: 获取用户数接口
     * @param: [request, response]
     * @return: java.lang.String
     * @version v1.0
     */
    public String getUserCount(HttpServletRequest request, HttpServletResponse response) throws Exception {
        int count = userDao.count();
        response.getWriter().print(count);
        return null;
    }

    /**
     * @auther: 子墨
     * @date: 2018/11/9 11:38
     * @describe: 获取商品浏览量接口
     * @param: [request, response]
     * @return: java.lang.String
     * @version v1.0
     */
    public String getProductBrowseCount(HttpServletRequest request, HttpServletResponse response) throws Exception {
        int count = 0;
        List<Product> products = productDao.findAll();
        for (Product product : products) {
            count += product.getPbrowse();
        }
        response.getWriter().print(count);
        return null;
    }

    /**
     * @auther: 子墨
     * @date: 2018/11/9 11:48
     * @describe: 获取订单数接口
     * @param: [request, response]
     * @return: java.lang.String
     * @version v1.0
     */
    public String getOrderCount(HttpServletRequest request, HttpServletResponse response) throws Exception {
        int count = orderDao.count();
        response.getWriter().print(count);
        return null;
    }

    /**
     * @auther: 子墨
     * @date: 2018/11/9 16:32
     * @describe: 获取商品分类比例接口
     * @param: [request, response]
     * @return: java.lang.String
     * @version v1.0
     */
    public String getCategoryProportion(HttpServletRequest request, HttpServletResponse response) throws Exception {

        List<Category> categories = categoryDao.findAll();
        for (Category c : categories) {
            int count = productDao.countAllbyCid(c.getCid());
            c.setProductCount(count);
        }
        String json = JsonUtils.listToJson(categories);
        System.out.println(json);
        response.getWriter().print(json);
        return null;
    }

    /**
     * @auther: 子墨
     * @date: 2018/11/9 20:56
     * @describe: 获取用户购买力接口
     * @param: [request, response]
     * @return: java.lang.String
     * @version v1.0
     */
    public String getUsersAndOrders(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<User> users = userDao.findAll();
        for (User u : users) {
            int count = orderDao.countByUid(u.getUid());
            u.setOrderCount(count);
        }
        // 过滤敏感信息
        String[] excludes = new String[]{
                "uid",
                "password",
                "email",
                "telephone",
                "birthday",
                "class",
                "code",
                "sex",
                "state"
        };
        JsonConfig config = JsonUtils.configJson(excludes);
        JSONArray jsonArray = JSONArray.fromObject(users, config);
        String json = jsonArray.toString();
        response.getWriter().print(json);
        return null;
    }
}
